package com.gitee.huxing.course.conc.conc01.op;

/**
 * @Description:
 * @ProjectName: JavaCourse
 * @Package: com.gitee.huxing.course.conc.cono01.join
 * @ClassName: Join
 * @Author: huxing
 * @DateTime: 2021-08-20 上午10:26
 */
public class Join {

    public static void main(String[] args) {
        Object object = new Object();
        MyThread thread1 = new MyThread("thread1 --");
        thread1.setObject(object);
        thread1.start();
        synchronized (thread1){
            for (int i = 0; i< 100; i++){
                if (i == 20){
                    try {
                        object.wait(0);
                    } catch (InterruptedException ex){
                        ex.printStackTrace();
                    }
                }
                System.out.println(Thread.currentThread().getName() + " --" + i);
            }
        }
    }
}

class MyThread extends Thread{

    private String name;

    private Object object;

    public void setObject(Object object) {
        this.object = object;
    }

    public MyThread(String name){
        this.name = name;
    }

    @Override
    public void run() {
        synchronized (object){
            for (int i = 0; i < 100; i++){
                System.out.println(name + i);
            }
        }
    }
}
